package com.example.a22310.myapplication.ViewModel.Personal;


import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.Toast;

import com.example.a22310.myapplication.Activity.SystemNews;
import com.example.a22310.myapplication.Adapter.Drag.RecycleItemTouchHelper;
import com.example.a22310.myapplication.Adapter.MvvmAdapter;
import com.example.a22310.myapplication.Beans.user;
import com.example.a22310.myapplication.Listener.ClubListener;
import com.example.a22310.myapplication.Model.UserModel;
import com.example.a22310.myapplication.R;
import com.example.a22310.myapplication.Service.service;
import com.example.a22310.myapplication.databinding.ActivitySystemNewsBinding;

import org.w3c.dom.Comment;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.provider.ContactsContract.CommonDataKinds.Website.URL;
import static com.example.a22310.myapplication.Adapter.Drag.RecycleItemTouchHelper.DELETEFLAG;

public class SystemNewsViewModel  implements ClubListener<Comment> {
    private ActivitySystemNewsBinding binding;
    private AppCompatActivity activity;
    private int userid;
    boolean flag = false;
    private int com_pid;
    List<Comment.ComListBean> comListBeen;

    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        public void run() {
            this.update();
            handler.postDelayed(this, 500);// 间隔1秒
        }

        void update() {
            if (DELETEFLAG == true) {
                delete();
                DELETEFLAG = false;
            }
        }
    };


    public SystemNewsViewModel(AppCompatActivity activity, ActivitySystemNewsBinding binding, int userid, int com_pid) {
        this.activity = activity;
        this.binding = binding;
        this.userid = userid;
        this.com_pid = com_pid;
        binding.setModel(this);
        UserModel userModel = new UserModel();
        userModel.scomment(userid, 1, com_pid, SystemNewsViewModel.this);
        handler.postDelayed(runnable, 500);
        flag = true;

    }

    public SystemNewsViewModel(SystemNews systemNews, ActivitySystemNewsBinding activitySystemNewsBinding, int activity, int binding) {
    }

    public void back(View view) {
        activity.onBackPressed();
        activity.finish();
    }

    public void post(View view) {


        binding.systemNews.setBackgroundResource(R.color.colorDeepBlue);
        binding.comment.setBackgroundResource(R.color.green);
        binding.systemNews.setTextColor(activity.getResources().getColor(R.color.green));
        binding.comment.setTextColor(activity.getResources().getColor(R.color.white));

        UserModel userModel = new UserModel();
        userModel.scomment(userid, 1, com_pid, SystemNewsViewModel.this);


    }

    public void system(View view) {


        binding.systemNews.setBackgroundResource(R.color.green);
        binding.comment.setBackgroundResource(R.color.colorDeepBlue);
        binding.systemNews.setTextColor(activity.getResources().getColor(R.color.white));
        binding.comment.setTextColor(activity.getResources().getColor(R.color.green));


        UserModel userModel = new UserModel();
        userModel.scomment(-1, 0, com_pid, SystemNewsViewModel.this);
        flag = false;
    }

    @Override
    public void onSuccess(Comment comment) {
        if (comment.getCode() != 404) {
            comListBeen = comment.getComList();
            if (comListBeen != null) {
                binding.recyclerViewNews.setLayoutManager(new LinearLayoutManager(activity));
                MvvmAdapter adapter = new MvvmAdapter(activity, R.layout.cardview_system_news, comListBeen, BR.comment);
                binding.recyclerViewNews.setAdapter(adapter);
                ItemTouchHelper.Callback callback = new RecycleItemTouchHelper(adapter);
                ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
                itemTouchHelper.attachToRecyclerView(binding.recyclerViewNews);


            }
        }

    }

    @Override
    public void onFailure() {
        Toast.makeText(activity, "连接服务器失败", Toast.LENGTH_LONG).show();
    }


    private void delete() {

        int a = comListBeen.get(pos).getCommentid();
        int fflag = 0;
        int uid = -99;
        if (flag == true) {
            fflag = 1;
            uid = userid;
        } else {
            fflag = 0;
            uid = -1;
        }
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(URL)
                .build();
        service service = retrofit.create(service.class);
        Call<user> call = service.delComment(uid, fflag, a);
        //3
        call.enqueue(new Callback<user>() {
            @Override
            public void onResponse(Call<user> arg0, Response<user> arg1) {
                // TODO Auto-generated method stub
                user user = arg1.body();
                if (user.getSuccess() == 1) {
                    Toast.makeText(activity, "删除成功", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(activity, "删除失败", Toast.LENGTH_LONG).show();
                }

            }

            @Override
            public void onFailure(Call<user> arg0, Throwable arg1) {
                // TODO Auto-generated method stub
                //Toast

            }
        });
    }
}